package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Struct;

public class Test {

    public static void reflectPrivateMethod(){
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Method method = c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);

            Student student = (Student) c1.newInstance();

            method.invoke(student,"调用私有方法");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void reflectPrivateField() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Field field = c1.getDeclaredField("name");
            //私有的要处理一下
            field.setAccessible(true);

            Student student = (Student) c1.newInstance();

            field.set(student,"lisi");

            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");
            Constructor<Student> constructor =
                    (Constructor<Student>)
                            c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);//处理私有成员

            Student student = constructor.newInstance("lisi",20);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectNewInstance() {
        try {
            Class<?> c1 = Class.forName("reflectdemo.Student");

            Student s1 = (Student) c1.newInstance();

            System.out.println(s1);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance();A
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }

    public static void main1(String[] args){
        try {
            //第一种方法
            Class<?> e1 = Class.forName("reflectdemo.Student");
            //第二种方法
            Class  e2 = Student.class;
            //第三种方法
            Student student = new Student();
            Class<?> e3 = student.getClass();

            //class对象只有一个
            System.out.println(e1.equals(e2));
            System.out.println(e1.equals(e3));
            System.out.println(e2.equals(e3));

        }catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
